/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.meruvian.yama.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.meruvian.yama.entity.Item;
import org.meruvian.yama.form.ItemForm;
import org.meruvian.yama.service.ItemService;
import org.meruvian.yama.validator.ItemValidator;
import org.meruvian.yama.wrapper.ItemListWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

/**
 *
 * @author vick
 */
@Controller
public class ItemController{

    @Autowired private ItemService itemService;

    @RequestMapping(value="/item/search/result",method=RequestMethod.POST)
    public ModelAndView result(@RequestParam("nameSearch") String name){
        StringBuilder builder = new StringBuilder();
        builder.append("result");
        if(!name.equalsIgnoreCase("")){
        	builder.append("/" + name);    	
        }
        ModelAndView mv = new ModelAndView(new RedirectView(builder.toString()));
        return mv;
    }

    @RequestMapping(value="/item/search/result", method=RequestMethod.GET)
    public ModelAndView search(HttpServletRequest request,HttpServletResponse response) throws Exception {
        ItemListWrapper items = new ItemListWrapper();
        items.setItems(itemService.getItems());
        ModelAndView mv = new ModelAndView("item/item-list");
        mv.addObject("list",items);
        return mv;
    }
    
    @RequestMapping(value="/item/search/result/{name}", method=RequestMethod.GET)
    public ModelAndView index(@PathVariable("name") String name){
        ModelAndView mv = new ModelAndView();
        ItemListWrapper items = new ItemListWrapper();
        items.setItems(itemService.findItems(name));
        mv.addObject("list",items);
        mv.setViewName("/item/item-list");
        return mv;
    }

    @RequestMapping(value="/item", method=RequestMethod.GET)
    public ModelAndView item(HttpServletRequest request, HttpServletResponse response) throws Exception{
        ModelAndView mv = new ModelAndView();
        mv.addObject("item", new Item());
        mv.setViewName("item/item-add");
        return mv;
    }
    
    @RequestMapping(value="/item/search", method=RequestMethod.GET)
    public ModelAndView searchItem(HttpServletRequest request, HttpServletResponse response) throws Exception{
        ModelAndView mv = new ModelAndView();
        mv.addObject("item", new Item());
        mv.setViewName("item/item-search");
        return mv;
    }

    @RequestMapping(value="/item", method=RequestMethod.POST)
    public ModelAndView item(HttpServletRequest request, @ModelAttribute("item") ItemForm item, BindingResult bindingResult){
    	new ItemValidator().validate(item, bindingResult);
    	if(bindingResult.hasErrors()){
            ModelAndView mv = new ModelAndView();
            mv.addObject("item", item);
            mv.setViewName("item/item-add");
            return mv;
        }else{
            itemService.save(item.getItem());
            return new ModelAndView(new RedirectView(request.getContextPath()+"/item/search/result"));
        }

    }

    @RequestMapping(value="/item/update/{id}", method=RequestMethod.GET)
    public ModelAndView updateItem(@PathVariable("id") String itemId){
        Item item = itemService.getItem(itemId);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("item/item-edit");
        mv.addObject("item",item);
        return mv;
    }

    @RequestMapping(value="/item/update/{id}", method=RequestMethod.POST)
    public ModelAndView updateItem(HttpServletRequest request, @ModelAttribute("item") ItemForm item, BindingResult bindingResult){
    	System.out.println(item.getName());
    	new ItemValidator().validate(item, bindingResult);
    	if(bindingResult.hasErrors()){
            ModelAndView mv = new ModelAndView();
            mv.addObject("item", item);
            mv.setViewName("item/item-edit");
            return mv;
        }else{
            itemService.save(item.getItem());
            return new ModelAndView(new RedirectView(request.getContextPath()+"/item/search/result"));
        }
    }

    @RequestMapping(value="/item/{id}/detail", method=RequestMethod.GET)
    public ModelAndView viewItem(@PathVariable("id") String itemId){
        Item item = itemService.getItem(itemId);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("item/item-view");
        mv.addObject(item);
        return mv;
    }

    @RequestMapping(value="/item/delete/{id}", method=RequestMethod.GET)
    public ModelAndView deleteItem(HttpServletRequest request,@PathVariable("id") String itemId){
        Item item =  itemService.getItem(itemId);
        itemService.delete(item);
        return new ModelAndView(new RedirectView(request.getContextPath()+"/item/search/result"));
    }

}
